#include "adc_driver.h"

adc_driver_t adc_driver;

static float k[] = {
	(1.0f / 4096.f) * _2PI,
};

uint8_t driver_num = sizeof(k) / sizeof(float);
uint16_t adc_buff[sizeof(k) / sizeof(float)];

/**
 * @brief  初始化
 * @param  
 * @retval 无
 */
void adc_driver_init(void)
{
	HAL_ADCEx_Calibration_Start(&hadc1);
	HAL_Delay(20);
}

/**
 * @brief  取模
 * @param  
 * @retval 无
 */
static inline float fmodf_pos(float x, float y)
{
    float out = fmodf(x, y);
    if (out < 0.0f){
        out += y;
	}
    return out;
}

/**
 * @brief  刷新驱动
 * @param  
 * @retval 无
 */
void adc_driver_update(void)
{
	static uint8_t who_doing = 0;
	
	if (who_doing == 0) {
		who_doing = 1;
		
		for (uint8_t i=0; i < driver_num; i++) {
			HAL_ADC_Start(&hadc1);
			HAL_ADC_PollForConversion(&hadc1, 50);
			
			adc_buff[i] = HAL_ADC_GetValue(&hadc1);
		}
		
		HAL_ADC_Stop(&hadc1);
		return;
	}
	
	
	if (who_doing == 1) {
		who_doing = 0;
		
		/* 处理数据 */
		adc_driver.angle = fmodf_pos(adc_buff[0] * k[0], _2PI);
		
		return;
	}
		
}
/******************* (C) COPYRIGHT 2025 FUYEBING*****END OF FILE************/
